package com.ufox.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ufox.entity.ProdDetails;
import com.ufox.entity.Products;
import com.ufox.entity.Result;
import com.ufox.entity.User;
import com.ufox.req.ProductsCondREQ;
import com.ufox.service.IProdDetailsService;
import com.ufox.service.IProductsService;
import com.ufox.utils.AliyunProperties;
import com.ufox.utils.AliyunUtil;
import com.ufox.utils.PicUtil;
import com.ufox.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 优狐科技-www.hbufox.com
 * @since 2022-06-09
 */
@RestController
@RequestMapping("/products")
public class ProductsController {

    @Autowired
    private IProductsService productsService;

    @Autowired
    private IProdDetailsService prodDetailsService;

//    @Value("${products.imgurl}")
//    private String imgurl;
//
//    @Value("${picUrl}")
//    private String picUrl;

//    @Autowired
//    private RedisUtils redisUtils;

    @Autowired
    private AliyunProperties aliyunProperties;
    //上传图书封面
//    @PostMapping("/shangchuan")
//    public Result shangchuan(MultipartFile touxiang) throws IOException {
//        String picUrl = PicUtil.getPicUrl(touxiang, imgurl, this.picUrl);
//        return Result.ok(picUrl);
//    }

    @PostMapping("/shangchuan")
    public Result upload(MultipartFile file){
        return  AliyunUtil.uploadFileToOss(file,aliyunProperties);
    }

    @GetMapping
    public Result interceptorUse() {
        return  Result.ok(productsService.list());
    }


    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Products products = productsService.getById(id);

        LambdaQueryWrapper<ProdDetails> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProdDetails::getProdId,id);

        ProdDetails prodDetails = prodDetailsService.getOne(wrapper);
        if (prodDetails != null) {
            products.setDetails(prodDetails.getDetails());
        }else {
            products.setDetails("");
        }
        return Result.ok(products);
    }

    @PostMapping("/{page}/{size}")
    public Result findAllCond(@RequestBody ProductsCondREQ productsCond, @PathVariable int page, @PathVariable int size) {
        Page<Products> pageInfo = new Page<>(page, size);
        Page<Products> list = productsService.selectAllByCond(pageInfo, productsCond);
        return Result.ok(list);
    }

    //添加  或 修改
    @PostMapping
    public Result addProducts(@RequestBody Products products,HttpServletRequest request) {

        return productsService.saveAndUpdate(products);
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delProducts(@PathVariable Integer id) {
        boolean b = productsService.removeById(id);
        return  (b ? Result.ok() : Result.error(""));
    }

    //批量删除

    @PostMapping("/batch")
    public Result delProductsBatch(@RequestBody List<Integer> ids) {
        boolean b = productsService.removeByIds(ids);
        return  (b ? Result.ok() : Result.error(""));
    }



    @GetMapping("/prod/{id}")
    public Result findProdById(@PathVariable String id) {
        boolean b = productsService.findProdById(id);
        return  (b ? Result.ok() : Result.error(""));
    }

    //根据用户id获取商品信息 带分页显示
    @PostMapping("/user/{id}/{page}/{size}")
    public Result findProdByUser(@RequestBody ProductsCondREQ productsCond,@PathVariable String id, @PathVariable int page, @PathVariable int size) {
        Page<Products> pageInfo = new Page<>(page, size);
        Page<Products> list = productsService.findProdByUser(id,pageInfo, productsCond);
        return Result.ok(list);
    }

    //根据用户id获取商品信息 不带分页
    @GetMapping("/findProdByUserId/{userId}")
    public Result findProdByUserId(@PathVariable String userId) {
        LambdaQueryWrapper<Products> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Products::getUserId,userId);
        List<Products> list = productsService.list(wrapper);
        return Result.ok(list);
    }

    @PostMapping("/prodState/{state}/{id}")
    public Result updateProdStateById(@PathVariable String state,@PathVariable String id) {
        boolean b = productsService.updateProdStateById(state,id);
        return  (b ? Result.ok() : Result.error(""));
    }


    @GetMapping("/type/{page}/{size}/{typeid}")
    public Result selectProductsByTypeId(@PathVariable int page,@PathVariable int size,@PathVariable String typeid){
        IPage<Products> pageInfo = new Page<>(page, size);
        return productsService.selectProductsByTypeId(pageInfo,typeid);
    }

    @GetMapping("/details/{id}")
    public Result selectDetailById(@PathVariable String id) {
        return productsService.selectDetailById(id);
    }

    @PostMapping("/name/{page}/{size}")
    public Result selectProductsByName(@PathVariable int page,@PathVariable int size,@RequestBody Products products){
        IPage<Products> pageInfo = new Page<>(page, size);
        return productsService.selectProductsByName(pageInfo,products.getName());
    }


}

